package com.training1.core.common.utils.framework;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.training1.core.common.global.Global;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @description 推送接口
 * @author hl
 * @date 2015年10月14日下午8:16:21
 */
public class PushUtil {
	
//	private static ExecutorService executorService = Executors.newCachedThreadPool();
	
	private static final Logger log = LoggerFactory.getLogger(PushUtil.class);
		
	/**
	 * 推送接口
	 * @param cellphone 号码
	 * @param title 标题
	 * @param msg 内容
	 * @return
	 */
	public static void push(final String cellphone,final String msg){
		
		push(cellphone, msg,1);
	}
	
	public static void push(final String cellphone,final String msg,int x){
//		executorService.execute(new Runnable() {
//			
//			@Override
//			public void run() {
				log.debug("申请推送：号码:{},内容:{}");
				String appKey =  Global.getConfig("jpush.appKey");//"87ade33847f169f3a21ca3d9";
				String masterSecre =  Global.getConfig("jpush.master.secret");//"9f2c1e7b5a285a53ae18aa57";//
				JPushClient client = new JPushClient(masterSecre, appKey);
				PushPayload pushPayload = PushPayload.newBuilder()
						.setPlatform(Platform.all())//android+ios+wp
						.setAudience(Audience.alias(cellphone))//别名
						.setNotification(Notification.alert(msg))
						.build();
				
				PushResult result;
				try {
					result = client.sendPush(pushPayload);
				} catch (Exception e) {
					log.error("推送失败",e);
					log.error("推送失败,号码:{},错误信息:{}",cellphone,e.getMessage());
					return;
				}
				if(result.isResultOK()){
					log.debug("推送成功,号码{}",cellphone);
				}else{
					log.debug("推送失败,号码{},返回码{}",cellphone,result.getResponseCode());
				}
//			}
//		});
	}
	
	public static void main(String[] args) {
//		System.out.println(push("xxx", "msg"));
	}
	
}

